package controller.business.fundAdminSelect;

import base.BaseEntity;
import base.response.OffsetLimitPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import controller.common.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.business.InspectorFundServices;
import services.business.InspectorInfoServices;
import services.dictionary.FundSetServices;
import util.DateUtil;
import wt.entity.business.InspectorFund;
import wt.entity.business.InspectorFundExample;
import wt.entity.business.InspectorInfo;
import wt.entity.business.InspectorInfoExample;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("business/fundAdminSelect")
public class FundAdminSelectController extends BaseController {


    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    InspectorInfoServices inspectorInfoServices;
    @Autowired
    FundSetServices fundSetServices;

    @Autowired
    InspectorFundServices inspectorFundServices;


    @RequiresPermissions("business:fundAdminSelect:default")
    @RequestMapping("index")
    public String index() {
        return "business/fundAdminSelect/index";
    }


    @RequiresPermissions("business:fundAdminSelect:default")
    @ResponseBody
    @RequestMapping("select")
    public OffsetLimitPage select(
            String expertName,
            String idCard,
            String bankName,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            Integer offset,
            Integer limit
    ) throws Exception {

        InspectorInfoExample inspectorInfoExample = new InspectorInfoExample();
        InspectorInfoExample.Criteria criteria1 = inspectorInfoExample.createCriteria();
        if (!StringUtils.isBlank(expertName)) {
            criteria1.andExpertNameLike("%" + expertName + "%");
        }
        if (!StringUtils.isBlank(idCard)) {
            criteria1.andExpertIdCardLike("%" + idCard + "%");
        }
        if (!StringUtils.isBlank(bankName)) {
            criteria1.andBankNameLike("%" + bankName + "%");
        }

        List<InspectorInfo> inspectorInfoList = inspectorInfoServices.selectByExample(inspectorInfoExample);
        if (inspectorInfoList.isEmpty()){
            return new OffsetLimitPage(new ArrayList(),0L);
        }


        List<String> inspectorIdList = BaseEntity.getIdList(inspectorInfoList);

        InspectorFundExample inspectorFundExample = new InspectorFundExample();
        inspectorFundExample.setOrderByClause("CHECK_TIME DESC");
        InspectorFundExample.Criteria criteria = inspectorFundExample.createCriteria();
        criteria.andInspectorIdIn(inspectorIdList).andStatusEqualTo(1);

        if (startTime != null) {
            criteria.andCheckTimeGreaterThanOrEqualTo(startTime);
        }
        if (endTime != null) {
            criteria.andCheckTimeLessThan(DateUtil.moveDate(endTime, 1));
        }

        PageHelper.offsetPage(offset,limit);
        List<InspectorFund> inspectorFundList = inspectorFundServices.selectByExample(inspectorFundExample);
        inspectorFundServices.lazyLoadBusinessInfo(inspectorFundList);
        inspectorFundServices.lazyLoadInspectorInfo(inspectorFundList);
        return new OffsetLimitPage((Page)inspectorFundList);
    }


}
